//
//  OAuthViewController.swift
//  weibo_yaoye
//
//  Created by mac on 15/5/14.
//  Copyright (c) 2015年 mac. All rights reserved.
//

import UIKit
import SVProgressHUD
//--------------step <  32 建立OAuth控制器 和故事版  >--------nextsteplocation<baseVC>---
class OAuthViewController: UIViewController,UIWebViewDelegate{
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //        loadAuthPage()
    }
    //--------------step <  39 加快加载速度  >--------nextsteplocation<<#currentfile#>>---
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        loadAuthPage()
    }
    
    @IBAction func close() {
        //--------------step <  34  单机关闭按钮回到首页 >--------nextsteplocation<<#currentfile#>>---
        dismissViewControllerAnimated(true, completion: nil)
        SVProgressHUD.dismiss() //currentStep: 49
        
    }
    
    //--------------step <  35 计算属性  >--------nextsteplocation<<#currentfile#>>---
    var webView:UIWebView
        {
            return view as! UIWebView
    }
    //--------------step <  36 定义常量获取code 要使用的  >--------nextsteplocation<<#currentfile#>>---
    let WB_Client_Id = "33217303"
    let WB_Redirect_Uri = "http://baidu.com"
    let WB_Client_Secret = "18f3f2b537d7182a8539f4af6694bf4c"
    
    //--------------step <  37 加载授权界面  >--------nextsteplocation<<#currentfile#>>---
    private func loadAuthPage()
    {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=" + WB_Client_Id + "&redirect_uri=" + WB_Redirect_Uri
        let url = NSURL(string: urlString)!
        webView.loadRequest(NSURLRequest(URL: url))
        
    }
    //--------------step <  38 代理方法 遵守协议 控制界面跳转获取code授权码 >--------nextsteplocation<userAcount>---
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //取到完整字符串
        let urlString = request.URL!.absoluteString!
        // 是官方界面  //currentStep: 38
        if urlString.hasPrefix("https://api.weibo.com/")
        {   //加载
            return true
        }
        // 是否是回调地址 不是就不跳转 //currentStep: 38
        if !urlString.hasPrefix(WB_Redirect_Uri)
        {
            return false
        }
        //代码运行到这里必然是回调地址
        // quere过滤掉了主机地址 和 协议头
        let codeStr = "code="
        let query = request.URL!.query! //currentStep: 38
        //判断是拒绝还是允许
        if query.hasPrefix(codeStr)
        {
            let code = (query as NSString).substringFromIndex(codeStr.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
            println( "验证码是" + code)
            loadAccessToken(code);
        }
        else
        {
            println("不好意思 没有授权成功")
            //直接关闭视图控制器
            SVProgressHUD.dismiss()
            dismissViewControllerAnimated(true, completion: nil)
        }
        
        return false
    }
    
    private func loadAccessToken(code: String) //currentStep: 41 
    {
        let parmas = [
            "client_id" : WB_Client_Id,
            "client_secret" : WB_Client_Secret,
            "grant_type": "authorization_code",
            "code" : code,
            "redirect_uri" : WB_Redirect_Uri,
        ]

        
        //currentStep: 49
        userAcount.loadAccessToken(parmas, completion: { (account, error) -> () in
            
            if account != nil
            {
                
                shareduserAccount = account
                println(account)
                return
            }
            SVProgressHUD.showInfoWithStatus("网络不给力")
            println(error)
        })
   
    }
   //--------------step <  40 加载界面动画  >--------nextsteplocation<<#currentfile#>>---
    func webViewDidStartLoad(webView: UIWebView) {//40 展示
  
        SVProgressHUD.show()
   
    }

    func webViewDidFinishLoad(webView: UIWebView) {//40 消失
    
        SVProgressHUD.dismiss()

    }


}
